﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Newtonsoft.Json;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.IO;
using System.Globalization;
using System.Web;
namespace DarrenSoft.Core
{
    using DarrenSoft.Domain.Extends;
    using DarrenSoft.Domain.Entities;
    public static class FormatJsonExtension
    {  
        public static FormatJsonResult JsonFormat(this Controller c, object data, string[] exceptMemberName)
        {
            FormatJsonResult result = new FormatJsonResult();
            result.Data = data;
            result.ExceptMemberName = exceptMemberName;
            return result;
        }

        public static FormatJsonResult JsonFormat(this Controller c, object data)
        {
            return JsonFormat(c, data, null);
        }
       
    }
    public static class JsonHelper
    {


        public static string JsonFormat(object data, string[] ExceptMemberName)
        {
            StringWriter sw = new StringWriter();
            JsonSerializer serializer = JsonSerializer.Create(
                new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    NullValueHandling = NullValueHandling.Ignore,
                    ContractResolver = new NHibernateContractResolver(ExceptMemberName)
                }
                );
            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;
                serializer.Serialize(jsonWriter, data);
            }
            return sw.ToString();
        }
        public static string JsonFormat(object data)
        {
            return JsonFormat(data,null);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="c"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static List<QueryFilter> DesQueryFilter(string jsonFilter)
        {
            if (string.IsNullOrEmpty(jsonFilter))
                return null;
            return JsonConvert.DeserializeObject<List<QueryFilter>>(jsonFilter);
        }
        public static List<QueryAssociation> DesQueryAssociation(string jsonAssociation)
        {
            if (string.IsNullOrEmpty(jsonAssociation))
                return null;
            return JsonConvert.DeserializeObject<List<QueryAssociation>>(jsonAssociation);
        }
        public static string JsonGridFormat<T>(int pageIndex,int pageSize,int recordsCount, IList<T> table,string[] ExceptName)
        {
          
            JsonGridResult<T> instanceGrid = new JsonGridResult<T>();
            instanceGrid.PageIndex = pageIndex;
            instanceGrid.PageSize = pageSize;
            instanceGrid.RecordsCount = recordsCount;
            instanceGrid.ResultTable = table;
            return JsonFormat(instanceGrid,ExceptName);
            
        }
      
    }
    public class FormatJsonResult : ActionResult
    {
        public string[] ExceptMemberName { get; set; }
        public Object Data { get; set; }
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = "application/json";

            StringWriter sw = new StringWriter();
            JsonSerializer serializer = JsonSerializer.Create(
                new JsonSerializerSettings
                { 
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    NullValueHandling = NullValueHandling.Ignore,
                    ContractResolver = new NHibernateContractResolver(ExceptMemberName)
                }
                );

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;
                serializer.Serialize(jsonWriter, Data);
            }
            response.Write(sw.ToString());
        }
    }
}
